import 'package:flutter/material.dart';

extension SizeContext on BuildContext {
  double get width => MediaQuery.of(this).size.width;
  double get height => MediaQuery.of(this).size.height;
}

extension WidgetModifier on Widget {
  Widget padding({
    double top = 0,
    double right = 0,
    double left = 0,
    double bottom = 0,
    double all = 0,
  }) {
    var t = top == 0 ? all : top;
    var l = left == 0 ? all : left;
    var b = bottom == 0 ? all : bottom;
    var r = right == 0 ? all : right;

    return Padding(
      padding: EdgeInsets.fromLTRB(l, t, r, b),
      child: this,
    );
  }

  Widget action(dynamic onTap,
      {HitTestBehavior behavior = HitTestBehavior.deferToChild}) {
    return GestureDetector(
      onTap: onTap,
      behavior: behavior,
      child: this,
    );
  }

  Widget board({
    double top = 0,
    double left = 0,
    double bottom = 0,
    double right = 0,
    double all = 0,
    Color color = const Color.fromARGB(255, 133, 133, 133),
    double? width,
    double? minHeight,
    double? height,
    double radius = 0,
    double paddigLeft = 0,
    double paddigTop = 0,
    double paddigRight = 0,
    double paddigBottom = 0,
    double paddigAll = 0,
    Color bgColor = Colors.transparent,
  }) {
    var size = BorderSide(color: color);
    if (all > 0) {
      top = all;
      left = all;
      right = all;
      bottom = all;
    }

    var topSize = top > 0 ? size : BorderSide.none;
    var leftSize = left > 0 ? size : BorderSide.none;
    var rightSize = right > 0 ? size : BorderSide.none;
    var bottomSize = bottom > 0 ? size : BorderSide.none;

    if (paddigAll > 0) {
      paddigLeft = paddigAll;
      paddigTop = paddigAll;
      paddigRight = paddigAll;
      paddigBottom = paddigAll;
    }

    BoxConstraints? b;
    if (minHeight != null) {
      b = BoxConstraints(minHeight: minHeight);
    }
    return Container(
      width: width,
      height: height,
      padding: EdgeInsets.only(
        left: paddigLeft,
        top: paddigTop,
        right: paddigRight,
        bottom: paddigBottom,
      ),
      decoration: BoxDecoration(
          border: Border(
            top: topSize,
            left: leftSize,
            bottom: bottomSize,
            right: rightSize,
          ),
          borderRadius: radius > 0 ? BorderRadius.circular(radius) : null,
          color: bgColor),
      constraints: b,
      child: this,
    );
  }
}

extension WidgetExtension on Widget {
  // Padding 扩展
  Widget paddingAll(double padding) => Padding(
        padding: EdgeInsets.all(padding),
        child: this,
      );

  Widget paddingSymmetric({
    double horizontal = 0.0,
    double vertical = 0.0,
  }) =>
      Padding(
        padding: EdgeInsets.symmetric(
          horizontal: horizontal,
          vertical: vertical,
        ),
        child: this,
      );

  Widget paddingOnly({
    double left = 0.0,
    double top = 0.0,
    double right = 0.0,
    double bottom = 0.0,
  }) =>
      Padding(
        padding: EdgeInsets.only(
          left: left,
          top: top,
          right: right,
          bottom: bottom,
        ),
        child: this,
      );

  // Alignment 扩展
  Widget get center => Center(child: this);

  Widget align(AlignmentGeometry alignment) => Align(
        alignment: alignment,
        child: this,
      );

  // Container 相关扩展
  Widget backgroundColor(Color color) => Container(
        color: color,
        child: this,
      );

  Widget card({
    Color? color,
    double? elevation,
    ShapeBorder? shape,
    BorderRadius? borderRadius,
    EdgeInsetsGeometry? margin,
  }) =>
      Card(
        color: color,
        elevation: elevation,
        shape: shape,
        margin: margin,
        child: this,
      );

  // 手势扩展
  Widget onTap(VoidCallback? onTap) => GestureDetector(
        onTap: onTap,
        behavior: HitTestBehavior.opaque,
        child: this,
      );

  Widget onDoubleTap(VoidCallback? onDoubleTap) => GestureDetector(
        onDoubleTap: onDoubleTap,
        behavior: HitTestBehavior.opaque,
        child: this,
      );

  Widget onLongPress(VoidCallback? onLongPress) => GestureDetector(
        onLongPress: onLongPress,
        behavior: HitTestBehavior.opaque,
        child: this,
      );

  // 尺寸限制扩展
  Widget constrained({
    double? minWidth,
    double? maxWidth,
    double? minHeight,
    double? maxHeight,
  }) =>
      ConstrainedBox(
        constraints: BoxConstraints(
          minWidth: minWidth ?? 0.0,
          maxWidth: maxWidth ?? double.infinity,
          minHeight: minHeight ?? 0.0,
          maxHeight: maxHeight ?? double.infinity,
        ),
        child: this,
      );

  Widget width(double width) => SizedBox(width: width, child: this);
  Widget height(double height) => SizedBox(height: height, child: this);
  Widget size(double width, double height) =>
      SizedBox(width: width, height: height, child: this);

  // 装饰扩展
  Widget clip(BorderRadius radius) => ClipRRect(
        borderRadius: radius,
        child: this,
      );

  Widget border({
    Color color = Colors.black,
    double width = 1.0,
    BorderRadius? borderRadius,
  }) =>
      Container(
        decoration: BoxDecoration(
          border: Border.all(color: color, width: width),
          borderRadius: borderRadius,
        ),
        child: this,
      );

  // 可见性扩展
  Widget visible(bool visible) => Visibility(
        visible: visible,
        child: this,
      );

  Widget opacity(double opacity) => Opacity(
        opacity: opacity,
        child: this,
      );

  // 滚动视图扩展
  Widget scrollable({
    ScrollPhysics? physics,
    EdgeInsetsGeometry? padding,
    bool primary = false,
  }) =>
      SingleChildScrollView(
        physics: physics,
        padding: padding,
        primary: primary,
        child: this,
      );

  // 展开扩展
  Widget expanded({int flex = 1}) => Expanded(
        flex: flex,
        child: this,
      );

  Widget flexible({int flex = 1, FlexFit fit = FlexFit.loose}) => Flexible(
        flex: flex,
        fit: fit,
        child: this,
      );
}
